<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>完善资料</title>
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>

    <link rel="stylesheet" href="../js/libs/sui/sm.min.css">
    <link rel="stylesheet" href="../js/libs/sui/sm-extend.min.css">
    <link rel="stylesheet" href="../iconfont/iconfont.css">
    <link rel="stylesheet" href="../less/main.css"/>

    <script type='text/javascript' src='../js/libs/jquery-3.1.0.min.js' charset='utf-8'></script>
    <script type="text/javascript">
        var Zepto = jQuery;
    </script>
    <script type='text/javascript' src='../js/libs/sui/sm.js' charset='utf-8'></script>
    <script type='text/javascript' src='../js/libs/sui/sm-extend.min.js' charset='utf-8'></script>
    <script src="../js/libs/juicer-min.js"></script>
    <script src="../js/libs/jquery.md5.js"></script>
    <script src="../js/api.js"></script>
    <script src="../js/utils.js"></script>
    <script src="../js/common.js"></script>
</head>
<body class="person-info">
<div class="page">
    <div class="content bg-default p-t22">
        <!--头部-->
        <div class="head-no-toolbar bg-self-green no-left-icon">
            <span class="icon iconfont c-white p-l" onclick="javascript:closeWin();">&#xe61f;</span>
            <span class="c-white t-center">完善资料</span>
        </div>
        <!-- 头像-->
        <div class="por">
            <div class="por-title">
                上传头像
            </div>
            <div class="por-img">
                <img src="../images/aboutBMS.jpg" alt=""/>
            </div>
        </div>
        <div class="list-block c-gray">
            <ul>
                <li>
                    <div class="item-content">
                        <div class="item-inner">
                            <div class="item-title label">姓名</div>
                            <div class="item-input c-gray">
                                <input id="stu-name" type="text" class="t-right" placeholder="请输入您的姓名">
                            </div>
                        </div>
                    </div>
                </li>
                <li>
                    <div class="item-content">
                        <div class="item-inner">
                            <div class="item-title label">身份证</div>
                            <div class="item-input c-gray">
                                <input id="stu-num" type="email" class="t-right" placeholder="请输入您的身份证号">
                            </div>
                        </div>
                    </div>
                </li>
                <li>
                    <div class="item-content">
                        <div class="item-inner">
                            <div class="item-title label">手机号</div>
                            <div class="item-input c-gray">
                                <input id="stu-phone" type="text" class="t-right" placeholder="请输入您的手机号">
                            </div>
                        </div>
                    </div>
                </li>
                <li>
                    <div class="item-content">
                        <div class="item-inner">
                            <div class="item-title label">QQ</div>
                            <div class="item-input c-gray t-right">
                                <input id="stu-qq" type="text" class="t-right" placeholder="请输入您的QQ号">
                            </div>
                        </div>
                    </div>
                </li>
                <li>
                    <div class="item-content">
                        <div class="item-inner">
                            <div class="item-title label">微信</div>
                            <div class="item-input c-gray t-right">
                                <input id="stu-wx" type="text" class="t-right" placeholder="请输入您的微信号">
                            </div>
                        </div>
                    </div>
                </li>
                <li id="school-flag-li">
                    <div class="item-content">
                        <div class="item-inner">
                            <div class="item-title label select-title">是否在校生</div>
                            <div class="item-input c-gray">
                                <select id="stu-school-flag" onchange="selectChg()">
                                    <option>是</option>
                                    <option>否</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </li>
                <li id="unit-li">
                    <div class="item-content">
                        <div class="item-inner">
                            <div class="item-title label">工作单位</div>
                            <div class="item-input c-gray">
                                <input id="stu-job-unit" type="text" class="t-right" placeholder="请输入您的工作单位">
                            </div>
                        </div>
                    </div>
                </li>
                <li id="job-li">
                    <div class="item-content">
                        <div class="item-inner">
                            <div class="item-title label">岗位</div>
                            <div class="item-input c-gray">
                                <input id="stu-job" type="text" class="t-right" placeholder="请输入您所在单位工作岗位">
                            </div>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
        <div class="content-block">
            <div class="row">
                <div class="col-100"><a id="update-info-btn" class="button button-big button-round button-fill bg-self-green">确定</a></div>
            </div>
        </div>
    </div>
</div>

<script>

    apiready=function(){
        juicer.set({
            'tag::operationOpen': '{@',
            'tag::operationClose': '}',
            'tag::interpolateOpen': '&{',
            'tag::interpolateClose': '}',
            'tag::noneencodeOpen': '$${',
            'tag::noneencodeClose': '}',
            'tag::commentOpen': '{#',
            'tag::commentClose': '}',
            'strip': false,
            'cache': false
        });

        // 获取登录用户
        var tmpUserInfo = getLoginUserInfo();
        $('.por-img img').attr('src',"{0}upload/{1}".format(getUrl(), tmpUserInfo.PHOTO_PATH));


        //获取登录用户
        var userInfo = getLoginUserInfo();

        var staffid = userInfo.STAFF_ID;

        if(isObject(userInfo)){
            //查询学生信息
            var infosql="SELECT * FROM s_student WHERE STAFF_ID='"+staffid+"'";
            dbQuery(1,1,infosql,true,function(ret,err){

                if(isObject(ret)&&ret.statusCode==0){
                    if(ret.result.length>0){
                        var stuInfo=ret.result[0];
                        $("#stu-name").val(stuInfo.STUDENT_NAME);
                        $("#stu-num").val(stuInfo.ID_NUM);
                        $("#stu-phone").val(stuInfo.MOBILE_PHONE);
                        $("#stu-qq").val(stuInfo.QQ);
                        $("#stu-wx").val(stuInfo.WX_CODE);
                        var flag=stuInfo.SCHOOL_FLAG;
                        if(flag==1){
                            $("#stu-school-flag").val("是");
                            jobShow(false);
                        }else{
                            $("#stu-school-flag").val("否");
                            jobShow(true);
                        }

                        $("#stu-job-unit").val(stuInfo.COMPANY_INFO);
                        $("#stu-job").val(stuInfo.POST_INFO);
                    }else{
                        toast("暂无数据");
                    }


                }
            });

        }

        //修改信息
        $("#update-info-btn").click(function(){
            updateInfo(staffid);
        });


        //更新信息
        function updateInfo(staffid){
            var stuName= $api.trim($("#stu-name").val());
            var stuNum= $api.trim($("#stu-num").val());
            var stuPhone= $api.trim($("#stu-phone").val());
            var stuQQ= $api.trim($("#stu-qq").val());
            var stuWx= $api.trim($("#stu-wx").val());
            var stuFlag= $("#stu-school-flag").val()=="是"?1:0;
            var stuUnit= $api.trim($("#stu-job-unit").val());
            var stuJob= $api.trim($("#stu-job").val());

            //如果是在校生 工作单位和岗位为空
            if(stuFlag){
                stuUnit="";
                stuJob="";
            }

            //修改su_staff 和 s_student 两张表
            var updatesql = "UPDATE su_staff s1,s_student s2" +
                    " SET s1.STAFF_NAME = '"+stuName+"'," +
                    "s1.MOBILE_PHONE = '"+stuPhone+"'," +
                    "s1.QQ = '"+stuQQ+"'," +
                    "s1.WX_CODE = '"+stuWx+"'," +
                    "s2.STUDENT_NAME = '"+stuName+"'," +
                    "s2.ID_NUM = '"+stuNum+"'," +
                    "s2.MOBILE_PHONE = '"+stuPhone+"'," +
                    "s2.QQ = '"+stuQQ+"'," +
                    "s2.WX_CODE = '"+stuWx+"'," +
                    "s2.SCHOOL_FLAG = '"+stuFlag+"'," +
                    "s2.COMPANY_INFO = '"+stuUnit+"'," +
                    "s2.POST_INFO = '"+stuJob+"'" +
                    " WHERE" +
                    " s1.STAFF_ID = '"+staffid+"'" +
                    " AND s1.STAFF_ID = s2.STAFF_ID";
            dbExecute(updatesql,true,function(ret,err){
                if(isObject(ret)&&ret.statusCode==0){
                    //更改登陆信息
                    userInfo.STAFF_NAME=stuName;
                    userInfo.MOBILE_PHONE=stuPhone;
                    userInfo.QQ=stuQQ;
                    userInfo.WX_CODE=stuWx;
                    // 保存登录信息
                    setStaffInfo("loginUserInfo", userInfo);


                    // 保存学生数据
                    var studentInfo=getLoginStudentInfo();
                    studentInfo.STUDENT_NAME = stuName;
                    studentInfo.ID_NUM = stuNum;
                    studentInfo.MOBILE_PHONE = stuPhone;
                    studentInfo.QQ = stuQQ;
                    studentInfo.WX_CODE = stuWx;
                    studentInfo.SCHOOL_FLAG =stuFlag;
                    studentInfo.COMPANY_INFO = stuUnit;
                    studentInfo.POST_INFO = stuJob;
                    setStaffInfo("loginStudentInfo", studentInfo);


                    //发送事件
                    api.sendEvent({
                        name: 'infoUpdateEvent'
                    });

                    toast("修改成功");
                }else{
                    toast("修改失败");
                }
            });
        }

        //是否显示工作单位和岗位
        function jobShow(isShow){
            if(isShow){
                $("#unit-li").show();
                $("#job-li").show();
            }else{
                $("#unit-li").hide();
                $("#job-li").hide();
            }
        }
        function selectChg(){
            var isShow = $("#stu-school-flag").val() == "是" ? false : true;
            jobShow(isShow);
        }

        $('.person-info .por').click(function(){
            $('body').append("<div class='pup'>\
                <ul>\
                    <li class='rel'>从相册选择</li>\
                    <li class='rel'>拍照</li>\
                    <li class='myCancel'>取消</li>\
                </ul>\
                </div>");
        });

        $('body').on('click','.pup li',function(){
            var index=$(this).index();
            var li_length=$(this).parent().find('li').length;
            if(index+1==li_length){
                $(this).css('background','#E3E3E3');
                $('body').find('.pup').remove();
            }else{
                $(this).css('background','#E3E3E3');

                //相册选择
                if(index==0){

                    getTouPicture("library");

                }else{  //相机
                    getTouPicture("camera");
                }

                $('body').find('.pup').remove();
            }
        });

        // 修改头像 SQL
        var updateHeadImgSql = "UPDATE SU_STAFF T SET T.PHOTO_PATH = '{0}' WHERE T.STAFF_ID = '{1}'";
        // 上传头像的标志：true-可以上传头像；false-不可以上传头像
        var uploadHeadImgFlag = true;

        function getTouPicture(type){
            if (uploadHeadImgFlag && isObject(userInfo)) {
                getPicture(300, 300, type, function (ret, err) {
                    if (isObject(ret)) {
                        var url = ret.data;
                        var uuid = getUUID();

                        if (isNotEmpty(url)) {
                            uploadHeadImgFlag = false;

                            // 上传头像
                            uploadHeadImage(userInfo.STAFF_ID, url, uuid, true, function (ret, err, remotePath) {
                                // 上传成功
                                if (isObject(ret) && (ret.statusCode == 0)) {
                                    var headImgPath = ret.result;

                                    // 修改头像
                                    dbExecute(updateHeadImgSql.format(headImgPath, userInfo.STAFF_ID), true, function (ret, err) {
                                        uploadHeadImgFlag = true;

                                        // 执行成功
                                        if (isObject(ret) && (ret.statusCode == 0)) {
                                            toast("上传头像成功");

                                            userInfo.PHOTO_PATH = headImgPath;
                                            $('.por-img img').attr('src',"{0}upload/{1}".format(getUrl(), userInfo.PHOTO_PATH));
                                            // 重新缓存登录用户信息
                                            setStaffInfo("loginUserInfo", userInfo);

                                            //发送事件
                                            api.sendEvent({
                                                name: 'infoUpdateEvent'
                                            });
                                            // 执行失败
                                        } else {
                                            setTimeout(function () {
                                                toast("上传头像失败，稍后再试");
                                            }, 3000);
                                        }
                                    });
                                } else {
                                    uploadHeadImgFlag = true;

                                    setTimeout(function () {
                                        toast("上传头像失败，稍后再试");
                                    }, 3000);
                                }
                            });
                        } else {
                            uploadHeadImgFlag = true;
                        }
                    } else {
                        uploadHeadImgFlag = true;
                    }
                });
            }
        }
    };

</script>
</body>
</html>